package com.site_assembly.apiImpl;

import com.service.AssignOrderApiService;
import com.service.form.OrderForm;
import com.service.form.RecycleForm;
import com.service.form.SiteForm;
import com.site_assembly.common.GetIdUtil;
import com.site_assembly.config.DeadLetterRabbitConfig;
import com.util.Result;
import io.swagger.annotations.*;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@Api(tags = "子站点端（派单页面）接口", description = "提供子站点（派单页面）相关的 Rest API")
public class AssignOrderApiImpl implements AssignOrderApiService {


    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private GetIdUtil getIdUtil;

    @Override
    @ApiOperation("某子站点未接单订单分页查询")
    public Result findOrderByPageAndOrderStateAndSiteId(@ApiParam(name = "orderState", value = "订单状态") String orderState,
                                                        @ApiParam(name = "current", value = "当前页", example = "1") Integer current,
                                                        @ApiParam(name = "pageSize", value = "页容量", example = "1") Integer pageSize) {
        //解析请求头jwt获取站点id
        Integer siteId = getIdUtil.getId();

        return Result.success();
    }

    @Override
    @ApiOperation("某子站点未接单认证通过骑手分页查询")
    public Result findRecycleByPageAndRecycleWorkStateAndRecyclerApproveState(@ApiParam(name = "recyclerApproveState", value = "认证状态") String recyclerApproveState,
                                                                              @ApiParam(name = "recycleWorkState", value = "骑手状态") String recycleWorkState,
                                                                              @ApiParam(name = "current", value = "当前页", example = "1") Integer current,
                                                                              @ApiParam(name = "pageSize", value = "页容量", example = "1") Integer pageSize) {

        //解析请求头jwt获取站点id
        Integer siteId = getIdUtil.getId();

        return Result.success();
    }

    @Override
    @ApiOperation("子站点未接单订单派单")
    @ApiResponses({@ApiResponse(code = 20000, message = "派单修改成功"), @ApiResponse(code = 40000, message = "派单修改失败")})
    public Result setOrderOfRecyclerIdByOrderSerialNumber(@ApiParam(name = "recyclerId", value = "分配骑手id", example = "1") Integer recyclerId,
                                                          @ApiParam(name = "orderSerialNumber", value = "订单编号") String orderSerialNumber) {

        rabbitTemplate.convertAndSend(
                DeadLetterRabbitConfig.DL_FINISH_EXCHANGE,
                DeadLetterRabbitConfig.DL_FINISH_RK,
                "你的订单来啦",
                new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setExpiration("" + (60 * 1000));
                        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                        return message;
                    }
                });

        return Result.success();
    }
}
